/****************************************************************************
*
*					Functions of class SimpleFeature
*
****************************************************************************/

#include <string>
#include <iostream>
#include <fstream>
#include <vector>
#include <stdlib.h>

#include "DataSet.h"
#include "SimpleFeature.h"


/****************************************************************************
*					SimpleFeature::SimpleFeature
*
*	Description:	Standard constructor with initialization of internal variables
*	Parameters:		-
*	Returns:		-
*
****************************************************************************/
SimpleFeature::SimpleFeature (void) {
	this->clear();
	this->id = 1;
	this->name.assign("Simple");
}


/****************************************************************************
*					SimpleFeature::~SimpleFeature
*
*	Description:	Standard destructor with removing all dynamic internal variables
*	Parameters:		-
*	Returns:		-
*
****************************************************************************/
SimpleFeature::~SimpleFeature (void) {
	
}


/****************************************************************************
*					SimpleFeature::calculate
*
*	Description:	Calculates feature value given variable domain and the data
*	Parameters:		varIDs - indexes of variables in 'vars' for which to 
*							 calculate the feature value
*					vars - all variables in the PGM
*	Returns:		0 - if features is not true (values of variables in PGM are
*						different from 'varValues')
*					1 - otherwise
*
****************************************************************************/
float SimpleFeature::calculate (const std::vector<unsigned int>& varIDs, const std::vector <unsigned int>& vars) const {
	if (varIDs.size() != varValuesSize) {
		std::cerr << "Error in SimpleFeature::calculate: number of variables is not equal to the corresponding number of values.";
		return -1;
	}
	for (unsigned int i = 0; i < varValuesSize; i++) {
		if (vars[varIDs[i]] != varValues[i])
			return 0.0;
	}
	return 1.0;
}


/****************************************************************************
*					SimpleFeature::calculate
*
*	Description:	Calculates feature value given variable domain and the data
*	Parameters:		varIDs - indexes of variables in 'vars' for which to 
*							 calculate the feature value
*	Returns:		0 - if features is not true (values of variables in PGM are
*						different from 'varValues')
*					1 - otherwise
*
****************************************************************************/
float SimpleFeature::calculate (const std::vector <unsigned int>& vars) const {
	if (vars.size() != varValuesSize) {
		std::cerr << "Error in SimpleFeature::calculate(vars): number of variables is not equal to the corresponding number of values.";
		return -1;
	}
	for (unsigned int i = 0; i < varValuesSize; i++) {
		if (vars[i] != varValues[i])
			return 0.0;
	}
	return 1.0;
}


/****************************************************************************
*					SimpleFeature::setDesiredValues
*
*	Description:	Sets values for which a feature should return 1
*	Parameters:		desiredValues - ordered vector of desired values
*	Returns:		0
*
****************************************************************************/
int SimpleFeature::setDesiredValues (const std::vector <unsigned int>& desiredValues) {
	varValues = desiredValues;
	varValuesSize = desiredValues.size();
	return 0;
}


/****************************************************************************
*					SimpleFeature::getDesiredValues
*
*	Description:	Returns values for which a feature gives 1
*	Parameters:		desiredValues - a parameter that is set to the feature's
*						desired values of a domain of variables
*	Returns:		0
*
****************************************************************************/
int SimpleFeature::getDesiredValues (std::vector <unsigned int>& desiredValues) const {
	desiredValues = varValues;
	return 0;
}


/****************************************************************************
*					SimpleFeature::getObjectID
*
*	Description:	Returns ID of this type of features
*	Parameters:		-
*	Returns:		int - id of this class
*
****************************************************************************/
int SimpleFeature::getObjectID (void) const {
	return this->id;
}


/****************************************************************************
*					SimpleFeature::getObjectName
*
*	Description:	Returns string Name of this type of features
*	Parameters:		name - name of this class
*	Returns:		0
*
****************************************************************************/
int SimpleFeature::getObjectName (std::string &nameStr) const {
	nameStr = this->name;
	return 0;
}


/****************************************************************************
*					SimpleFeature::clear
*
*	Description:	Removes all variables in object internal variables
*	Parameters:		-
*	Returns:		-
*
****************************************************************************/
void SimpleFeature::clear (void) {
	varValues.clear();
	varValuesSize = 0;
}


/****************************************************************************
*					SimpleFeature::operator==
*
*	Description:	Operator for checking whether two featres are the same
*	Parameters:		-
*	Returns:		-
*
****************************************************************************/
bool SimpleFeature::operator==(Feature& second) const {
	if (this->id != second.getObjectID())
		return false;
	SimpleFeature* sec; 
	sec = dynamic_cast<SimpleFeature*> (&second);
	if (this->varValuesSize != sec->varValuesSize)
		return false;
	for (unsigned int i = 0; i < varValuesSize ; i++) {
		if (this->varValues[i] != sec->varValues[i])
			return false;
	}
	return true;
}
